<?php
class Bbs extends TagsAbstract {
	/**
	 * 是否是封闭的标签
	 * @var unknown_type
	 */
	static $TAG_CLOSED = false;
	
	public $config = array();
	
	public function __construct(){

	}
	public function getTagStatus(){
		return self::$TAG_CLOSED;
	}
	/**
	 * 返回模板文件路径
	 */
	public function getTemplateFile($tpl = '') {
		//返回需要渲染的模板
		$file = $this->attr ['style'];
	if(!empty($tpl)){
			$file = $tpl;
		}
		return dirname(__FILE__).'/bbs/'.$file.'.html';
	}
	
	/**
	 * 设置使用哪个Widget,暂时无用。。
	 */
	public function setWidget() {
		return 'test';
	}
	
	/**
	 * 这里返回的是模板中需要渲染的变量
	 */
	public function replace($checkCache) {
		$order  = isset($this->attr['order']) ? $this->attr['order']		 : 'id DESC';
		$limit  = isset($this->attr['limit']) ? intval($this->attr['limit']) : 20;
		!isset($this->attr['words']) && $this->attr['words'] = 10;
		
		$source = $this->attr['source'];
		
		$map			= array();
		$map['isdel']	= 0;
		switch($source) {
			case 'board':
				if($this->attr['board'] == 'ALL') break;
				$map['class2'] = array('in', $this->attr['board']);
				if(!empty($this->attr['cate']) && $this->attr['cate'] != 'ALL') {
					$map['tclass'] = array('exp', 'IN (' . $this->attr['cate'] . ')');
				}
				break;

			case 'cate':
				if(!empty($this->attr['cate']) && $this->attr['cate'] != 'ALL') {
					$map['tclass'] = array('exp', 'IN (' . $this->attr['cate'] . ')');
				}
				break;

			case 'user':
				//支持uid和工号(字母+数字)的混编
				$umodel = M('user');
				$users  = explode(',', $this->attr['user']);
				$uname = array();
				foreach($users as $k => $v) {
					if(!is_numeric($v)) {
						$uname[] = $v;
						unset($users[$k]);
					}
				}
				if(!empty($uname)){
					$tempMap['uname'] = array('in',$uname);
					$tempUser = $umodel->where($tempMap)->field('uid')->findAll();
					foreach($tempUser as $value){
						$users[] = $value['uid'];
					}
				}
				$users = array_unique($users);
				$map['uid'] = array('in', $users);
				break;
			
			case 'ids':
				$map['id'] = array('in', $this->attr['ids']);
				break;
			
			default:
				
		}

		//根据类型过滤（1:精华贴   2:附件贴   3:置顶贴）
		if(!empty($this->attr['status'])) {
			$status = explode(',', $this->attr['status']);
			in_array('1', $status) && $map_status[] = 'dist   = 1';
			in_array('2', $status) && $map_status[] = 'attach = 1';
			in_array('3', $status) && $map_status[] = 'top    = 1';
			$map['_string'] = implode(' OR ', $map_status);
		}

		$model = D('bbs_topic');
		$res   = $model->where($map)->order($order)->findPage($limit);
		$list  = $res['data'];
		
		//追加内容
		if($this->attr['style'] == "top_2"){
			$tids = getSubByKey($list, 'id');
			$tempMap['tid'] = $tids[0];
			$tempMap['isdel']   = 0;
			$topic = M('bbs_post')->field('id,content,tid')->where($tempMap)->find();
			$list[0]['content']=$topic['content'];
		}
		

		//用户的姓名
		$uids = $this->_format($res['data'], 'uid');
		$sql  = 'SELECT uid, fullname FROM ' . C('DB_PREFIX') . 'user WHERE `uid` IN ( ' . implode(',', $uids) . ' ) LIMIT ' . count($uids);
		$temp = D('')->query($sql);
		foreach($temp as $v) {
			$names[$v['uid']] = $v['fullname'];
		}
		
		$tag = model('Xtag');
		$tempData = array();
		foreach($list as $k => $v) {
			$list[$k]['uname'] = $names[$v['uid']];
			$cid = array();
			$cid[] = $v['class1'];
			$cid[] = $v['class2'];
			$cid[] = $v['class3'];
			$temp = array_filter($cid);
			$cid = implode(',',$temp);
			$list[$k]['category']['id'] = $cid;
			$temp2 = array_pop($temp);
			$tempData[$k]['id'] = $k;
			$tempData[$k]['cid'] = $temp2;
		}
		$tempData2 = group($tempData,'cid');
		$tagId = array_keys($tempData2);
		//查询出所有的tag
		$sql = "SELECT tagname,tagid from `ts_x_tag` where tagId IN ('".implode(',',$tagId)."')";
		$tagData = M('')->query($sql);
		$tagData = group($tagData,'tagid');
		foreach($tempData2 as $k=>$v){
			foreach($v as $so){
				$list[$so['id'] ]['category']['name'] = $tagData[$so['cid']][0]['tagname'];
			}
		}
		$var['title'] = t($this->attr['title']);
		$var['list']  = $list;
		$var['html']  = $res['html'];
		$var['attr']  = $this->attr;
		$var['image'] = $this->attr['image'];
		return $var;
	}


	/** ********************* 自定义函数 ********************* **/

	//将二维数组格式化为一维数组
	protected function _format($arr, $field) {
		$res = array();
		foreach($arr as $v) {
			$res[] = $v[$field];
		}
		return $res;
	}
}
?>